<template>
  <div class="app-container">
    <div class="header-box">
      <div class="header-left">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="商家名称" prop="storeName" v-if="this.activeName == 'store'">
            <el-input
              v-model="queryParams.storeName"
              placeholder="请输入商家名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="代理商名称" prop="agentName" v-else>
            <el-input
              v-model="queryParams.agentName"
              placeholder="请输入代理商名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="消费时间" prop="daterangeCreateTime">
            <el-date-picker style="width: 210px;" v-model="daterangeCreateTime" value-format="yyyy-MM-dd" type="daterange"
              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div class="button-right">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <el-divider></el-divider>
    <el-button
      type="primary"
      icon="el-icon-download"
      size="mini"
      @click="handleExport"
      v-hasPermi="['storeExpend:storeExpend:export']"
      style="float: right;"
    >导出</el-button>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="商家" name="store">
        <el-table v-loading="loading" :data="storeExpendList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" align="center" type="index" width="50" />
          <el-table-column label="序号" align="center" prop="id" width="80" /
          <el-table-column label="交易ID" align="center" prop="exchangeMark" />
          <el-table-column label="商家名称" align="center" prop="storeName" />
          <el-table-column label="所属代理商" align="center" prop="agentName" />
          <el-table-column label="消费金额" align="center" prop="exchangeAmount" />
          <el-table-column label="消费后余额" align="center" prop="balance" />
          <el-table-column label="交易信息" align="center" prop="content" width="400"  />
          <el-table-column label="消费时间" align="center" prop="createTime" width="200">
    <!--        <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template> -->
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="代理商" name="agent">
        <Agent ref="son"></Agent>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script>
import { listStoreExpend, getStoreExpend, delStoreExpend, addStoreExpend, updateStoreExpend } from "@/api/storeExpend/storeExpend";
import Agent from "../storeExpend/Agent";
export default {
  name: "StoreExpend",
  components: {
    Agent
  },
  data() {
    return {
      daterangeCreateTime: [],
      activeName: 'store',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家消费明细表格数据
      storeExpendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exchangeMark: [
          { required: true, message: "交易标识id，十位数字和字母组成不能为空", trigger: "blur" }
        ],
        storeId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        agentId: [
          { required: true, message: "代理商id不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "消费类型 0代理商充值 1商家充值 2商家发起背调扣费 3对应代理商接口调用扣费不能为空", trigger: "change" }
        ],
        exchangeAmount: [
          { required: true, message: "交易金钱数额不能为空", trigger: "blur" }
        ],
        balance: [
          { required: true, message: "交易后余额不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 切换标签页
    handleClick() {
      this.queryParams.pageNum = 1;
      if( this.activeName=='agent' ){
        this.$refs.son.getList(this.queryParams)
      }
      else{
        this.getList();
      }
    },
    // 时间处理
    setTime() {
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
    },
    /** 查询商家消费明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listStoreExpend(this.queryParams).then(response => {
        this.storeExpendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        exchangeMark: null,
        storeId: null,
        storeName: null,
        agentId: null,
        type: null,
        exchangeAmount: null,
        balance: null,
        content: null,
        dataId: null,
        imgUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.setTime();
      this.queryParams.pageNum = 1;
      if(this.activeName=='agent'){
        this.$refs.son.getList(this.queryParams)
      }else{
        this.getList();
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家消费明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStoreExpend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家消费明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreExpend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreExpend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商家消费明细编号为"' + ids + '"的数据项？').then(function() {
        return delStoreExpend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storeExpend/storeExpend/export', {
        ...this.queryParams
      }, `storeExpend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
